;+
;  NAME:
;    TMPFILE
;
;  PURPOSE:
;    Create a temporary file.
;
;  CALLING SEQUENCE:
;	s = tmpfile(prefix=prefix, suffix=suffix)
;
;	Returns a unique filename for scratch use.
;	Ex:  myfile = tmpfile(prefix='/tmp/', suffix='.tmp')
;       
;  Revision History:
;   Creator: Dave Johnston
;   Name was not unique enough.  Sometimes when two programs called this
;   one within one second, they could get the same file name. 
;   Cleaned up the code some.
;         18-Jan-2005 Erin Sheldon UofChicago
;
;-
;
;
;
;  Copyright (C) 2006  Dave Johnston
;
;    This program is free software; you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation; either version 2 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with this program; if not, write to the Free Software
;    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
;
;

function tmpfile, prefix=prefix, suffix=suffix, help=help

  if keyword_set(help) then begin
      doc_library, 'tmpfile'
      return, 0
  endif 

  if not keyword_set(prefix) then prefix = 'tmpfile-'
  if not keyword_set(suffix) then suffix = ''
  
  repeat begin
      
      numstr =  $
        strtrim(string(ulong64(1000*systime(1))), 2) + '-' + $
        strtrim(string(long(1000000*randomu(seed))), 2)
      
      file = prefix + numstr + suffix
      
  endrep until not fexist(file)
  
  return, file
  
end 
